<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');

include 'securities.php';

class Manage_posts extends Securities {

	function __construct() {
		parent::__construct('manage_posts');
		$this -> form_validation -> set_error_delimiters('<span class="error">', '</span>');
	}

	public function index() {
		$data['title'] = $this->lang->line('title_list_posts');
		$data['controller_name'] = strtolower(get_class());
		$data['manage_datatable'] = get_posts_manage_table($this -> mod_globals -> select_all(PREFIX_TABLE . 'posts'), $this -> uri -> segment(1));
		$this -> load -> view(B_MASTER . 'index', $data);

	}

	public function insert_post() {
		$module_id = $this -> uri -> segment(1);
		if (!$this -> is_has_modify_permission($module_id)) {
			redirect('ctm-common/no-allow-modify/' . $module_id);
		}

		$data['title'] = $this->lang->line('title_new_post');

		$this -> form_validation -> set_rules('tag_name', 'lang:form_label_tag_name', 'required');

		if ($this -> form_validation -> run() === FALSE) {
			$data['dropdown_tags'] = get_dropdown_tags($this -> mod_globals -> select_all(PREFIX_TABLE . 'tags'));
			$this -> load -> view(B_MASTER . 'index', $data);
		} else {
			$image_name = "";
			$path = "";
			$is_error = FALSE;
			if (isset($_FILES['userfile']) && $_FILES['userfile']['size'] > 0) {

				// user provided a file to upload
				$config['upload_path'] = './templates/front_end/images/tags/';
				$config['allowed_types'] = 'gif|jpg|jpeg|png|bmp';
				$config['max_size'] = '1000';
				$config['max_width'] = '1024';
				$config['max_height'] = '768';
				$this -> load -> library('upload', $config);

				if (!$this -> upload -> do_upload()) {
					$is_error = TRUE;
					// already checked for file from form, so this is a legit error
					$data['error'] = $this -> upload -> display_errors();
					$this -> load -> view(B_MASTER . 'index', $data);
				} else {
					// in here is where things went according to plan.
					// file is uploaded, people are happy
					$data = $this -> upload -> data();
					$image_name = $data['file_name'];
					$path = $data['file_path'];
				}
			}

			if (!$is_error) {
				$tag_name = $this -> input -> post('tag_name');
				$desc = $this -> input -> post('description');

				$is_saved = $this -> mod_globals -> insert(PREFIX_TABLE . 'posts', array('use_tag_id' => $tag_name, 'img_name' => $image_name, 'img_path' => $path, 'description' => $desc, 'status' => 1, 'created_date' => null));
				if ($is_saved) {
					$this -> session -> set_flashdata('success', 'Post  was created success.');
					redirect('manage-posts');
				} else {
					$this -> session -> set_flashdata('error', 'Post  was created fail.');
					redirect('manage-posts');
				}
			}

		}

	}

	public function update_post() {
		$module_id = $this -> uri -> segment(1);
		if (!$this -> is_has_modify_permission($module_id)) {
			redirect('ctm-common/no-allow-modify/' . $module_id);
		}

		$post_id = $this -> uri -> segment(3);
		$data['title'] = $this->lang->line('title_update_post');

		$this -> form_validation -> set_rules('tag_name', 'lang:form_label_tag_name', 'trim|required');

		if ($this -> form_validation -> run() === FALSE) {
			$data['post_data'] = $this -> mod_globals -> select_where(PREFIX_TABLE . "posts", array('post_id' => $post_id));
			$tag_id = $data['post_data'] -> row() -> use_tag_Id;
			$data['dropdown_tags_selected'] = get_dropdown_tags_select($this -> mod_globals -> select_all(PREFIX_TABLE . 'tags'), $tag_id);

			$this -> load -> view(B_MASTER . 'index', $data);
		} else {
			$image_name = "";
			$path = "";
			$is_error = FALSE;
			if (isset($_FILES['userfile']) && $_FILES['userfile']['size'] > 0) {

				// user provided a file to upload
				$config['upload_path'] = './templates/front_end/images/tags/';
				$config['allowed_types'] = 'gif|jpg|jpeg|png|bmp';
				$config['max_size'] = '1000';
				$config['max_width'] = '1024';
				$config['max_height'] = '768';
				$this -> load -> library('upload', $config);

				if (!$this -> upload -> do_upload()) {
					$is_error = TRUE;
					// already checked for file from form, so this is a legit error
					$data['error'] = $this -> upload -> display_errors();
					$this -> load -> view(B_MASTER . 'index', $data);
				} else {
					//unlink previous image

					$get_post = $this -> mod_globals -> select_where(PREFIX_TABLE . "posts", array('post_id' => $post_id));
					if ($get_post -> num_rows() > 0) {
						$image_name = $get_post -> row() -> img_name;
						$img_path = $get_post -> row() -> img_path;
						if ($image_name != "") {
							unlink($img_path . "" . $image_name);
						}

					}
					// in here is where things went according to plan.
					// file is uploaded, people are happy

					$data = $this -> upload -> data();
					$image_name = $data['file_name'];
					$path = $data['file_path'];
				}
			} else {
				$get_post = $this -> mod_globals -> select_where(PREFIX_TABLE . "posts", array('post_id' => $post_id));
				if ($get_post -> num_rows() > 0) {
					$image_name = $get_post -> row() -> img_name;
					$path = $get_post -> row() -> img_path;

				}
			}

			if (!$is_error) {
				$tag_name = $this -> input -> post('tag_name');
				$desc = $this -> input -> post('description');

				$is_saved = $this -> mod_globals -> update(PREFIX_TABLE . 'posts', array('use_tag_id' => $tag_name, 'img_name' => $image_name, 'img_path' => $path, 'description' => $desc, 'status' => 1, 'created_date' => null), array('post_id' => $post_id));
				if ($is_saved) {
					$this -> session -> set_flashdata('success', 'Post  was updated success.');
					redirect('manage-posts');
				} else {
					$this -> session -> set_flashdata('error', 'Post  was updated fail.');
					redirect('manage-posts');
				}
			}

		}
	}

	public function delete_post() {
		$module_id = $this -> uri -> segment(1);
		if (!$this -> is_has_modify_permission($module_id)) {
			redirect('ctm-common/no-allow-modify/' . $module_id);
		}

		$post_id = $this -> uri -> segment(3);
		$get_post = $this -> mod_globals -> select_where(PREFIX_TABLE . "posts", array('post_id' => $post_id));
		if ($get_post -> num_rows() > 0) {
			$image_name = $get_post -> row() -> img_name;
			$img_path = $get_post -> row() -> img_path;
			if ($image_name != "") {
				unlink($img_path . "" . $image_name);
			}

		}

		$is_deleted = $this -> mod_globals -> delete(PREFIX_TABLE . "posts", array('post_id' => $post_id));
		if ($is_deleted) {
			$this -> session -> set_flashdata('success', 'Post  was deleted success.');
			redirect('manage-posts');
		} else {
			$this -> session -> set_flashdata('error', 'Post  was deleted fail.');
			redirect('manage-posts');
		}
	}
	
	public function load_post(){
		$items_per_group = 5;
		//sanitize post value
	$group_number = filter_var($_POST["group_no"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);
	
	//throw HTTP error if group number is not valid
	if(!is_numeric($group_number)){
		header('HTTP/1.1 500 Invalid number!');
		exit();
	}
	
	//get current starting point of records
	$position = ($group_number * $items_per_group);
	
	//Limit our results within a specified range. 
	
	$results = mysql_query("SELECT id,name,message FROM paginate ORDER BY id ASC LIMIT $position, $items_per_group");
	
	if ($results) { 
		//output results from database
		
		while($obj = $results->fetch_object())
		{
			echo '<li id="item_'.$obj->id.'">'.$obj->id.' - <strong>'.$obj->name.'</strong></span> &mdash; <span class="page_message">'.$obj->message.'</span></li>';
		}
	
	}
	}

}
